package cn.lili.modules.grab.dingdong.facade;

import cn.lili.modules.grab.dingdong.reps.goods.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class DingdongGoodsFacede {

    /**
     * 构建DetailRespose对象 商品详情
     *
     * @param parentJson json对象
     */
    public DetailRespose detailResposeBuilder(JSONObject parentJson) {
        //商品
        DetailRespose.Goods goods = JSONObject.parseObject(parentJson.getString("goods"), DetailRespose.Goods.class);
        //商品规格
        List<DetailRespose.Specs> specs = JSONArray.parseArray(parentJson.getString("specs"), DetailRespose.Specs.class);
        //规格名称集合
        Map<String, String> specs_name = JSON.parseObject(parentJson.getString("specs_name"), new TypeReference<>() {});
        //规格名称集合
        Map<String, Map<String,DetailRespose.SpecsList>> specs_list = JSON.parseObject(parentJson.getString("specs_list"), new TypeReference<>() {});
        //图片集合
        List<DetailRespose.Images> images = JSONArray.parseArray(parentJson.getString("images"), DetailRespose.Images.class);

        //构建DetailRespose对象
        return DetailRespose.builder()
                .goods(goods)
                .specs(specs)
                .specs_name(specs_name)
                .specs_list(specs_list)
                .images(images)
                .build();
    }

    /**
     * 构建DetailRespose对象 商品配送方式
     *
     * @param parentJson json对象
     */
    public GetYfModeRespose getYfModeResposeBuilder(JSONObject parentJson) {
        //快递集合
        GetYfModeRespose.ShipId ship = parentJson.getJSONObject("ship")==null?null:JSONObject.parseObject(parentJson.getJSONObject("ship").getString("ship_id"), GetYfModeRespose.ShipId.class);
        GetYfModeRespose.ShipId wl = parentJson.getJSONObject("wl")==null?null:JSONObject.parseObject(parentJson.getJSONObject("wl").getString("wl_id"), GetYfModeRespose.ShipId.class);
        GetYfModeRespose.ShipId db_ship = parentJson.getJSONObject("db_ship")==null?null:JSONObject.parseObject(parentJson.getJSONObject("db_ship").getString("dbship_id"), GetYfModeRespose.ShipId.class);
        GetYfModeRespose.ShipId db_wl = parentJson.getJSONObject("db_wl")==null?null:JSONObject.parseObject(parentJson.getJSONObject("db_wl").getString("dbwl_id"), GetYfModeRespose.ShipId.class);
        GetYfModeRespose.ShipId jd_ship = parentJson.getJSONObject("jd_ship")==null?null:JSONObject.parseObject(parentJson.getJSONObject("jd_ship").getString("jdship_id"), GetYfModeRespose.ShipId.class);
        GetYfModeRespose.ShipId jd_wl = parentJson.getJSONObject("jd_wl")==null?null:JSONObject.parseObject(parentJson.getJSONObject("jd_wl").getString("jdwl_id"), GetYfModeRespose.ShipId.class);
        GetYfModeRespose.ShipId sf_ship = parentJson.getJSONObject("sf_ship")==null?null:JSONObject.parseObject(parentJson.getJSONObject("sf_ship").getString("sfship_id"), GetYfModeRespose.ShipId.class);

        return GetYfModeRespose.builder()
                .ship(ship)
                .wl(wl)
                .db_ship(db_ship)
                .db_wl(db_wl)
                .jd_ship(jd_ship)
                .jd_wl(jd_wl)
                .sf_ship(sf_ship)
                .build();
    }

    /**
     * 构建GetBrandRespose对象 商品品牌列表
     *
     * @param parentJson json对象
     */
    public GetBrandRespose getBrandResposeBuilder(JSONObject parentJson) {
        //品牌集合
        List<GetBrandRespose.List> list = JSONArray.parseArray(parentJson.getString("list"), GetBrandRespose.List.class);
        //当前页
        String page = parentJson.getString("page");
        //每页条数
        String pageSize = parentJson.getString("page_size");
        //总数
        String total = parentJson.getString("list_total");

        return GetBrandRespose.builder()
                .list(list)
                .page_size(pageSize)
                .page(page)
                .list_total(total)
                .build();
    }

    /**
     * 构建GoodsListV1Respose对象 新版商品列表接口（带总数）
     *
     * @param parentJson json对象
     */
    public GoodsListV1Respose goodsListV1ResposeBuilder(JSONObject parentJson) {
        //列表集合
        List<GoodsListV1Respose.List> list = JSONArray.parseArray(parentJson.getString("list"), GoodsListV1Respose.List.class);
        //总数
        String count = parentJson.getString("count");
        //当前页
        String current_page = parentJson.getString("current_page");
        //每页显示总数
        String quantity_page = parentJson.getString("quantity_page");

        return GoodsListV1Respose.builder()
                .list(list)
                .count(count)
                .current_page(current_page)
                .quantity_page(quantity_page)
                .build();
    }

    /**
     * 构建GetGoodsStockV1Respose对象 商品库存查询
     *
     * @param parentJson json对象
     */
    public GetGoodsStockV1Respose getGoodsStockV1ResposeBuilder(JSONObject parentJson) {
        //售后详情
        List<GetGoodsStockV1Respose.Detail> detail = JSONArray.parseArray(parentJson.get("detail").toString(), GetGoodsStockV1Respose.Detail.class);
        //商品ID
        String goods_id = parentJson.getString("goods_id");
        //该商品总数量
        String total = parentJson.getString("total");
        //订单状态文字描述
        String status_cn = parentJson.getString("status_cn");

        return GetGoodsStockV1Respose.builder()
                .detail(detail)
                .goods_id(goods_id)
                .total(total)
                .status_cn(status_cn)
                .build();
    }
}
